package net.gyapp.markim.module.contact.adapter;

import android.text.TextUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.easyder.wrapper.core.manager.ImageManager;

import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;
import net.gyapp.markim.module.contact.vo.GroupMemberVo;
import net.gyapp.markim.module.contact.vo.TempGroupBean;

import java.util.List;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/5.
 * Desc:
 */
public class GroupUserAdapter extends BaseQuickAdapter<Object, BaseRecyclerHolder> {
    public GroupUserAdapter() {
        super(R.layout.item_group_member);
    }

    public void setAdapterData(List<GroupMemberVo.GroupUsersBean> list) {
        mData.clear();
        if (list != null) {
            mData.addAll(list);
        }

        notifyDataSetChanged();
    }

    public void setAdapterData(List<GroupMemberVo.GroupUsersBean> list, boolean isCanAdd, boolean isCanReduce) {
        mData.clear();
        if (list != null) {
            int limit = 16;
            limit = isCanAdd ? limit - 1 : limit;
            limit = isCanReduce ? limit - 1 : limit;
            if (list.size() > limit) {
                List<GroupMemberVo.GroupUsersBean> beans = list.subList(0, limit);
                mData.addAll(beans);
            } else {
                mData.addAll(list);
            }
        }
        if (isCanAdd) {
            mData.add(new TempGroupBean(1, R.drawable.group_add));
        }
        if (isCanReduce) {
            mData.add(new TempGroupBean(2, R.drawable.group_jianshao));
        }
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseRecyclerHolder helper, Object item) {
        if (item instanceof GroupMemberVo.GroupUsersBean) {
            GroupMemberVo.GroupUsersBean.UserInfoBean userInfo = ((GroupMemberVo.GroupUsersBean) item).userInfo;
            if (userInfo != null) {
                helper.setText(R.id.tv_name, TextUtils.isEmpty(userInfo.nick) ? userInfo.userName : userInfo.nick);
                ImageManager.load(mContext, ((ImageView) helper.getView(R.id.iv_image)), ((GroupMemberVo.GroupUsersBean) item).userInfo.userAvatar, R.drawable.placeholder_header);
            } else {
                helper.setText(R.id.tv_name, String.valueOf("暂无昵称"));
                ImageManager.load(mContext, ((ImageView) helper.getView(R.id.iv_image)), "", R.drawable.placeholder_header);
            }

        } else if (item instanceof TempGroupBean) {
            helper.setText(R.id.tv_name, null);
            helper.setImageResource(R.id.iv_image, ((TempGroupBean) item).imageId);
        }


    }
}
